/**
 * Created by bin.zhang on 14-11-10.
 */

define(['jquery','underscore','backbone','global','../appDialog/dialogPage'
    ,'text!/android_asset/busAppWeb/module/busApp/busChange/busSearchResult.html'],
    function($,_,Backbone,global,dialogPage,html){
        var busSearchResultView = Backbone.View.extend({
            busLinePlanList:[],//公交换乘线路
            routeAndLineSite:[],//需要提醒的站点
            centPoint:null,//地图中心点
            template: _.template(html),
            initialize:function(data){
                this.render();
            },
            render:function(){
                $('#searchResultPage').html(this.template);
//                $('body').trigger("create");
            },
            search:function(start,end){
                this.centPoint = start;
                var _this = this;
                _this.busLinePlanList = [];
                var transit = new BMap.TransitRoute(global.map, {
                    renderOptions: {map: global.map}
                    ,onSearchComplete:function(result){
                        if (transit.getStatus() == BMAP_STATUS_SUCCESS){
                            // 输出方案信息
                            var lineInfoList = [];
                            for (var i = 0; i < result.getNumPlans(); i++){
                                _this.busLinePlanList[i] = result.getPlan(i);
                                var lineInfo = '<li><a href="#dialogPage" data-rel="dialog" id="'+i+'">';
                                lineInfo += "<h2>"+result.getPlan(i).getDistance(true) + "/"+result.getPlan(i).getDuration(true)+"</h2>"
                                lineInfo += "<p>"+result.getPlan(i).getDescription()+"</p></a>"
                                lineInfo += "<a href='#busMapPage' id='map"+i+"'>在地图中查看</a></li>";
                                lineInfoList.push(lineInfo);
                            }
                            $("#busSearchResult")[0].innerHTML = lineInfoList.join("");
                            $('body').trigger("create");
                        }
                        _this.addListener();
                    }
                });
                transit.search(start, end);
            },
            addListener:function(){
                var _this = this;
                $(document).ready(function(){
                    $('li a').click(function(){
                        if($(this)[0].id.indexOf("map") >= 0){
                            var id = $(this)[0].id.substr(3,1);
                            _this.drawLineMap(_this.busLinePlanList[id]);
                        }else{
                            var busLinePlan = _this.busLinePlanList[$(this)[0].id];
                            _this.setRemindSite(busLinePlan);
                        }
                    });
                });
            },
            drawLineMap:function(busPlan){
                // 绘制步行线路
                for (var i = 0; i < busPlan.getNumRoutes(); i++){
                    var walk = busPlan.getRoute(i);
                    if (walk.getDistance(false) > 0){
                        // 步行线路有可能为0
                        global.map.addOverlay(new BMap.Polyline(walk.getPath(), {lineColor: "green"}));
                    }
                }
                // 绘制公交线路
                for (var i = 0; i < busPlan.getNumLines(); i++){
                    var line = busPlan.getLine(i);
                    global.map.addOverlay(new BMap.Polyline(line.getPath()));
                }
                global.map.centerAndZoom(this.centPoint,15);
            },
            setRemindSite:function(busPlan){
                //清空以往记录
                this.routeAndLineSite = [];
                //记录步行关键点
                for(var i= 0; i < busPlan.getNumRoutes(); i++){
                    var walk = busPlan.getRoute(i);
                    for(var j = 0; j < walk.getNumSteps(); j++){
                        var step = walk.getStep();
                        this.routeAndLineSite.push({name:step.getDescription(false),point:step.getPosition()});
                    }
                }

                for(var i = 0; i < busPlan.getNumLines(); i++){
                    var line = busPlan.getLine(i);
                    this.routeAndLineSite.push({name:line.getGetOnStop().title,point:line.getGetOnStop().point});
                    this.routeAndLineSite.push({name:line.getGetOffStop().title,point:line.getGetOffStop().point});
                }
                this.initDialogPage();
            },
            initDialogPage:function(){
                var _this = this;
                var dialogSiteHtml = "<ul id='siteRemind' data-role='listview'>";
                for (var i = 0; i < this.routeAndLineSite.length; i++){
                    dialogSiteHtml += "<li> <a id='"+i+"'>"+ this.routeAndLineSite[i].name+"</a></li>";
                }
                dialogSiteHtml += " </ul>"
                var dialogPageView = new dialogPage();
                dialogPageView.setHeader("设置到站提醒");
                dialogPageView.setContent(dialogSiteHtml);
                dialogPageView.addListener();
                dialogPageView.remindRecord(function(id){
                    global.busSearchRemindList.push(_this.routeAndLineSite[id])
                    Backbone.history.navigate('#searchResultPage',true)
                });
            }


        });
        return busSearchResultView;
    }
)